/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.softjourn.punchcard.lib;

import android.content.Context;
import android.util.Log;

import com.softjourn.punchcard.R;
import com.softjourn.punchcard.model.Client;
import com.softjourn.punchcard.model.ClientResponse;
import com.softjourn.punchcard.model.OffersResponse;
import com.softjourn.punchcard.model.PunchCardsResponse;

import java.util.HashMap;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class ApiFunctions {
	
	private static String CLIEN_AUTHORIZATION_URL  = "client/merchant/{merchantUID}/email/{email}/pass/{pass}/authorization"; 
	private static String CLIEN_REGISTRATION_URL  = "client/merchant/{merchantUID}/registration"; 
	private static String PUNCHCARD_URL  = "client/{clientUID}/punchcards"; 
	private static String OFFERS_URL  = "client/{clientUID}/merchant/{merchantUID}/offers"; 
	

	private SessionManager session;
		
	private static String PUNCHCARD_API_URL = "http://punch.softjourn.if.ua/punchWS/";
	
	private String merchant_uid;

	
	// constructor
	public ApiFunctions(Context context) {
		
		PropertiesUtil propertiesUtil = new PropertiesUtil(context);
		merchant_uid = propertiesUtil.getProperty(context.getResources().getString(R.string.merchant_property));
		
		session = new SessionManager(context); 
		HashMap<String, String> systemDetails = session.getSystemDetails();
		PUNCHCARD_API_URL = systemDetails.containsKey(PUNCHCARD_API_URL) ? systemDetails.get(PUNCHCARD_API_URL) : "http://punch.softjourn.if.ua/punchWS/"; 
		
	}
		
	/**
	 * function make Login Request
	 * @param email
	 * @param password
	 * */
	public ClientResponse loginUser(String email, String password){
		Log.e("LOGIN USER", email +"  "+password);
	
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();

		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		ClientResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ CLIEN_AUTHORIZATION_URL, ClientResponse.class, merchant_uid, email, password);
		
		Log.i("LOGIN USER RESULT ", result.toString());
					
		return result;

	}
	
	
	/**
	 * function make Login Request
	 * @param name
	 * @param email
	 * @param password
	 * */
	public ClientResponse registerUser(String name, String email, String password){

		Log.e("REGISTRATION USER", email +"  "+password+" "+name);
		
		// Create client for registration
		Client client =  new Client();
		client.setEmail(email);
		client.setPass(password);
		client.setName(name);
		
		
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();


		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP POST request, marshaling the response to a String
		ClientResponse result = restTemplate.postForObject(PUNCHCARD_API_URL+ CLIEN_REGISTRATION_URL, client,  ClientResponse.class, merchant_uid);
		
		Log.e("REGISTRATION USER RESULT", result.toString());
		
		return result;
	}
	
	
	/**
	 * function return client's punch cards
	 * @param uid - client's UID
	 * */
	public PunchCardsResponse punchCards(String uid){

		Log.e("PUNCHCARD USER", uid);
				
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();


		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		PunchCardsResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ PUNCHCARD_URL, PunchCardsResponse.class, uid);
		
		Log.e("REGISTRATION USER RESULT", result.toString());
		
		return result;
	}
	
	
	/**
	 * function return client's punch cards
	 * @param uid - client's UID
	 * */
	public OffersResponse offers(String uid){

		Log.e("PUNCHCARD USER", uid);
				
		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();


		// Add the Jackson message converter
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

		// Make the HTTP GET request, marshaling the response to a String
		OffersResponse result = restTemplate.getForObject(PUNCHCARD_API_URL+ OFFERS_URL, OffersResponse.class, uid, merchant_uid);
		
		Log.e("REGISTRATION USER RESULT", result.toString());
		
		return result;
	}
	
	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context){
		if(!session.isLoggedIn()){
			return true;
		}
		return false;
	}
	
	/**
	 * Function to logout user
	 * Reset Database
	 * */
	public boolean logoutUser(Context context){
		session.logoutUser();
		return true;
	}
	
}
